<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */
class Modules_Upload_Services_Directory
{
	/**
	 * @param string $path Current upload path
	 * @return string
	 */		
	public static function directoryTreeview($dir = ROOT_DIR, $firstCall = true) {
		$objs = @scandir($dir);				
		natcasesort($objs);
		$dirs = array();
		foreach($objs as $obj) {
			if (is_dir($dir . DS . $obj)) {
				$dirs[] = $obj;
			}			
		}
		$objs 	 = $dirs;		
		$dirTree = '';
		if (count($objs) > 2 ) {
			$dirTree = '<ul';
			if ($firstCall) {
				$dirTree  .= ' class="g_directory_tree"'; 
				$firstCall = false; 
			}
			$dirTree .= '>';
			foreach ($objs as $obj) {				
				if (!in_array($obj, array('.', '..', '.svn'))) {
					if (is_dir($dir . DS . $obj)) {
						$dirTree .= '<li class="directory"><a href="javascript: void(0);" rel="' . self::getPathLink($dir . DS . $obj) . '">' . htmlspecialchars($obj) . '</a>';
						$dirTree .= self::directoryTreeview($dir . DS . $obj, false);
						$dirTree .= '</li>';
					}
				}
			}
			$dirTree .= '</ul>';
		}
		return $dirTree;
	}
	
	public static function getPathLink($path) 
	{
		return rawurlencode(base64_encode($path));
	}
}
